package ttt;

import io.vavr.Function1;
import io.vavr.Function2;
import io.vavr.Function5;
import io.vavr.control.Either;
import io.vavr.control.Option;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Stream;
import static io.vavr.API.*;

public class Main {
    public static void main(String[] args) {
        Lazy<String> stringSupplier = Lazy.of(() -> "7890");
        System.out.println(stringSupplier.get());
        Lazy<Integer> map = stringSupplier.map(Integer::parseInt);
        System.out.println(map.get());
        Lazy<Integer> integerLazy = map.flatMap((i) -> {
            if (i % 2 == 0) {
                return Lazy.of(() -> 0);
            }
            return Lazy.of(() -> 1);
        });
        System.out.println(integerLazy.get());

        Lazy< Long> param1Lazy = Lazy.of(() -> 2L);
        Lazy< Long> param2Lazy = Lazy.of(() -> 2L);
        Lazy< Long> param3Lazy = Lazy.of(() -> 2L);
        Lazy< Long> result = param1Lazy.flatMap(param1 ->
                param2Lazy.flatMap(param2 ->
                        param3Lazy.map(
                                param3 -> param1 + param2 + param3
                        )
                )
        );
        System.out.println(result.get());

        List<String> list = new ArrayList<>();
        Optional<List<String>> op = Optional.of(list);

        op.ifPresent(System.out::println);
        op.stream().map(a -> a).forEach(System.out::println);

        int i = (int) (Math.random() * 100);
        String s = Match(i).of(
                Case($(it -> it > 50), "NICE"),
                Case($(1), "CAO"),
                Case($(), "?")
        );
        System.out.println(s);

        Option<String> os = Match(i).option(
                Case($(0), "zero")
        );

        String s1 = os.getOrElse("nil");
        System.out.println(s1);

        Function5<Integer, Integer, Integer, Integer, Integer, Integer> sum = (a, b, c, d, e) -> a + b + c + d + e;
        Function2<Integer, Integer, Integer> add6 = sum.apply(2, 3, 1);

        System.out.println(add6.apply(4, 3) == 13);

        Either<String,Integer> value = compute().map(j -> j * 2);
        // 使用 fold 方法处理 Either
        String result1 = value.fold(
                left -> "Error: " + left,  // 处理左值（错误）
                right -> "Result: " + right  // 处理右值（成功的结果）
        );
        System.out.println(result1);
        System.out.println(value.getLeft());

        String result2 = Option.of("(String) null")
                .map(str -> (String) null)
                .flatMap(Option::of)
                .getOrElse(() -> "world");
//                .getOrElse("world");
        System.out.println(result2);

        List<Object> objects = new ArrayList<>();
        for (int j = 0; j < 1000; j++) {
            objects.add(j);
        }

        List<Object> java = io.vavr.collection.List.ofAll(objects).asJava();
        System.out.println("java:" + java);

        long start = System.currentTimeMillis();
        List<Object> java1 = io.vavr.collection.List.ofAll(
                Option.of((List<Object>)null).fold(() -> new ArrayList<>(), item -> item)
        ).asJava();
        System.out.println("java1:" + java1);
        System.out.println(System.currentTimeMillis() - start);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("1", 'v');
        map1.put("2", 'v');
        io.vavr.collection.Map<String, Object> vmap = io.vavr.collection.HashMap.ofAll(
                Option.of(
//                        (Map<String, Object>)null
                        map1
                ).fold(() -> new HashMap<>(), item -> item)
        );
        System.out.println("---");
        System.out.println(vmap);
        System.out.println(vmap.get("1").getOrElse("nil"));
        System.out.println(vmap.toList().get(1)._1);
        vmap.toStream().forEach(System.out::println);

        System.out.println("汉".getBytes(StandardCharsets.UTF_16).length);
        System.out.println("汉".getBytes(StandardCharsets.UTF_8)[0] + ":" + "汉".getBytes(StandardCharsets.UTF_8)[1] + ":" + "汉".getBytes(StandardCharsets.UTF_8)[2]);
    }

    public static Either<String, Integer> compute() {
        // 假设这里有一些计算逻辑
//        return Either.right(5); // 成功的结果
        // 或者
         return Either.left("Error message"); // 错误的情况
    }
}